package com.platform.modules.report.controller;

import com.platform.common.utils.MapUtil;
import com.platform.common.utils.NumberUtils;
import com.platform.modules.report.dao.CptAccReportDao;
import com.platform.modules.report.dto.BaseItemDTO;
import com.platform.modules.report.dto.BaseQueryDTO;
import com.platform.modules.report.dto.CptAccDistributeDTO;
import com.platform.modules.report.service.CptAccReportService;
import com.platform.modules.sys.dao.SysCityAreaDao;
import com.platform.modules.sys.entity.SysCityAreaEntity;
import com.platform.modules.sys.entity.SysDictEntity;
import com.platform.modules.sys.service.SysDictService;
import com.platform.web.utils.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags = "报表 - 竣工验收分析")
@RestController
@RequestMapping("report/cpt/acc")
public class CptAccReportController extends BaseReportController {


    @Autowired
    SysCityAreaDao sysCityAreaDao;
    @Autowired
    CptAccReportDao cptAccReportDao;
    @Autowired
    CptAccReportService cptAccReportService;
    @Autowired
    SysDictService sysDictService;

    @ApiOperation("今日数据 - 申请数c | 完成f | 驳回r")
    @PostMapping("today")
    public CommonResult<Map<String, Object>> today(){
        return CommonResult.success(MapUtil.of("cptAccCount",1000, "finishCount",567, "rejectCount",456));
    }

    @ApiOperation("竣工验收数 - 柱")
    @PostMapping("cptAccCountGroupByYear")
    public CommonResult<List<BaseItemDTO>> cptAccCountGroupByYear(@RequestBody  BaseQueryDTO baseQueryDTO){
        Map<String,Object> map = cptAccReportService.cptAccCountGroupByYear(baseQueryDTO);
        List<BaseItemDTO> data = this.buildStringItem(map, baseQueryDTO.getYearList());
        return CommonResult.success(data);
    }


    @ApiOperation("某年每个月竣工验收数 - 柱，必须传入年份")
    @PostMapping("cptAccCountGroupByMonth")
    public CommonResult<List<BaseItemDTO>> cptAccCountGroupByMonth(@RequestBody  BaseQueryDTO baseQueryDTO){
        Map<String,Object> map = cptAccReportService.cptAccCountGroupByMonth(baseQueryDTO);
        List<BaseItemDTO> data = this.buildStringItem(map, months);
        return CommonResult.success(data);
    }

    @ApiOperation("项目类型竣工验收数 - 柱")
    @PostMapping("cptAccCountGroupByPrjCategory")
    public CommonResult<List<BaseItemDTO>> cptAccCountGroupByPrjCategory(@RequestBody  BaseQueryDTO baseQueryDTO){
        Map<String,Object> map = cptAccReportService.cptAccCountGroupByPrjCategory(baseQueryDTO);
        List<SysDictEntity> dicts = sysDictService.queryByCode(MapUtil.of("code","PRJ_CATEGORY"));
        List<BaseItemDTO> data = this.buildDictItem(map,dicts);
        return CommonResult.success(data);
    }



    @ApiOperation("竣工驗收分析 - 默认:表格")
    @PostMapping("distribute")
    public CommonResult<List<CptAccDistributeDTO>> distribute(BaseQueryDTO baseQueryDTO){
        List<SysCityAreaEntity> areas = this.getAreas(baseQueryDTO.getProvinceNum());
        Map<String,Object> cptAccCountMap = cptAccReportService.cptAccCountGroupByCity(baseQueryDTO);
        Map<String,Object> cptAcPrjCountMap = cptAccReportService.cptAccPrjCountGroupByCity(baseQueryDTO);

        List<CptAccDistributeDTO> data = new ArrayList<>(areas.size());
        areas.forEach(e->{
            String xzqh = e.getId();
            Integer cptAccPrjCount = NumberUtils.intValueOf0(cptAcPrjCountMap.get(xzqh));
            Integer cptAccCount = NumberUtils.intValueOf0(cptAccCountMap.get(xzqh));
            data.add(new CptAccDistributeDTO(xzqh,e.getName(),cptAccPrjCount,cptAccCount,10000000,500,0.9F));
        });
        return CommonResult.success(data);
    }

}
